package user;

import vehicle.Bus;
import vehicle.Car;
import vehicle.Truck;
import vehicle.Vehicle;
import java.util.List;
import java.util.Scanner;

// VIP 用户类
public class VIPUser extends User {
    private Scanner scanner;

    public VIPUser(String username, String password) {
        super(username, password);
        this.scanner = new Scanner(System.in);
    }

    @Override
    public void operate(List<Vehicle> vehicleList) {
        boolean loop = true;
        while (loop) {
            System.out.println("VIP用户操作菜单：");
            System.out.println("1. 租车");
            System.out.println("2. 还车");
            System.out.println("3. 付款");
            System.out.println("4. 退出");
            try {
                int choice = scanner.nextInt();
                scanner.nextLine();
                switch (choice) {
                    case 1:
                        rentCar(vehicleList);
                        break;
                    case 2:
                        returnCar(vehicleList);
                        break;
                    case 3:
                        payRent(vehicleList);
                        break;
                    case 4:
                        loop = false;
                        break;
                    default:
                        System.out.println("无效选择，请重新输入。");
                }
            } catch (Exception e) {
                System.out.println("输入错误，请重新输入。");
                scanner.nextLine();
            }
        }
    }

    private void rentCar(List<Vehicle> vehicleList) {
        if (vehicleList.size() > 0) {
            System.out.println("可租用车辆：");
            for (Vehicle vehicle : vehicleList) {
                if (!vehicle.isRented()) {
                    System.out.println("车辆类型：" + vehicle.getClass().getSimpleName() + "，车牌：" + vehicle.getLicensePlate() + "，品牌：" + vehicle.getBrand());
                }
            }
            System.out.println("请输入要租用的车辆车牌：");
            String rentPlate = scanner.nextLine();
            for (Vehicle vehicle : vehicleList) {
                if (vehicle.getLicensePlate().equals(rentPlate)) {
                    if (!vehicle.isRented()) {
                        vehicle.rent();
                        System.out.println("请输入租车天数：");
                        int days = scanner.nextInt();
                        scanner.nextLine();
                        vehicle.setRentDays(days);
                        System.out.println("成功租用车辆：" + vehicle.getLicensePlate() + "，租车天数：" + days);
                        return;
                    } else {
                        System.out.println("车辆已被租用，请选择其他车辆。");
                        return;
                    }
                }
            }
            System.out.println("未找到指定车辆。");
        } else {
            System.out.println("无可用车辆。");
        }
    }

    private void returnCar(List<Vehicle> vehicleList) {
        System.out.println("请输入要归还的车辆车牌：");
        String returnPlate = scanner.nextLine();
        for (Vehicle vehicle : vehicleList) {
            if (vehicle.getLicensePlate().equals(returnPlate)) {
                if (vehicle.isRented()) {
                    vehicle.returnCar();
                    System.out.println("成功归还车辆：" + vehicle.getLicensePlate());
                    return;
                } else {
                    System.out.println("该车辆未被租用，无法归还。");
                    return;
                }
            }
        }
        System.out.println("未找到指定车辆。");
    }

    private void payRent(List<Vehicle> vehicleList) {
        int totalRent = 0;
        for (Vehicle vehicle : vehicleList) {
            if (vehicle.isRented()) {
                if (vehicle instanceof Car) {
                    totalRent += 50 * vehicle.getRentDays();
                } else if (vehicle instanceof Truck) {
                    if (vehicle.getRentDays() <= 5) {
                        totalRent += 100 * vehicle.getRentDays();
                    } else {
                        totalRent += 100 * 5 + 95 * (vehicle.getRentDays() - 5);
                    }
                } else if (vehicle instanceof Bus) {
                    if (vehicle.getRentDays() <= 5) {
                        totalRent += 80 * vehicle.getRentDays();
                    } else {
                        totalRent += 80 * 5 + (int) (80 * 0.8 * (vehicle.getRentDays() - 5));
                    }
                }
            }
        }
        if (totalRent > 0) {
            System.out.println("您本次需要支付的租金为：" + totalRent + "元，请前往线下收银台付款。");
        } else {
            System.out.println("您没有未支付的租金。");
        }
    }

}